package com.zhichat.web.controller;

import com.zhichat.infrastructure.valid.AddGroup;
import com.zhichat.infrastructure.valid.ModifyGroup;
import com.zhichat.service.GroupInfoService;
import com.zhichat.web.api.R;
import com.zhichat.web.dto.AddOrRemoveMemberDTO;
import com.zhichat.web.dto.GroupInfoAddOrUpdateDTO;
import com.zhichat.web.vo.GroupDetailAndMembersVO;
import com.zhichat.web.vo.GroupDetailVO;
import com.zhichat.web.vo.GroupListVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 群组管理
 *
 * @author: insanetz https://space.bilibili.com/602197686
 * @CreateDate: 2025/02/27 14:24
 */
@Slf4j
@Validated
@AllArgsConstructor
@RestController
@RequestMapping("/group")
public class GroupController {

    private final GroupInfoService groupInfoService;

    /**
     * 群 新增
     */
    @PostMapping("/create")
    public R<String> createGroup(@Validated(AddGroup.class) GroupInfoAddOrUpdateDTO groupInfoDTO) throws IOException {
        return R.status(groupInfoService.createGroup(groupInfoDTO));
    }

    /**
     * 群 修改
     */
    @PostMapping("/update")
    public R<String> updateGroup(@Validated(ModifyGroup.class) GroupInfoAddOrUpdateDTO groupInfoDTO) throws IOException {
        return R.status(groupInfoService.updateGroup(groupInfoDTO));
    }

    /**
     * 群列表 获取
     */
    @GetMapping("/list")
    public R<List<GroupListVO>> listGroup() {
        return R.success(groupInfoService.listGroup());
    }

    /**
     * 群 详情
     */
    @GetMapping("/detail")
    public R<GroupDetailVO> detail(@RequestParam("groupId") Long groupId) {
        return R.success(groupInfoService.detail(groupId));
    }

    /**
     * 群 所有成员
     */
    @GetMapping("/members")
    public R<GroupDetailAndMembersVO> detailAndMembers(@RequestParam("groupId") Long groupId) {
        return R.success(groupInfoService.detailAndMembers(groupId));
    }

    /**
     * 添加/删除 群成员
     */
    @PostMapping("/addOrRemoveMember")
    public R<String> addOrRemoveMember(@RequestBody @Validated AddOrRemoveMemberDTO addOrRemoveMemberDTO) {
        return R.status(groupInfoService.addOrRemoveMember(addOrRemoveMemberDTO));
    }

    /**
     * 退出群组
     */
    @GetMapping("/quitGroup")
    public R<String> quitGroup(@RequestParam("groupId") Long groupId) {
        return R.status(groupInfoService.quitGroup(groupId));
    }

    /**
     * 解散群组
     */
    @GetMapping("/disbandGroup")
    public R<String> disbandSelfGroup(@RequestParam("groupId") Long groupId) {
        return R.status(groupInfoService.disbandSelfGroup(groupId));
    }
}
